﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using System.Xml.Linq;
using Infrastructure.Aspect.Dynamic;

using Skynet.BusinessEntities;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

namespace Skynet.MVCWebSite.Areas.Administration.Controllers
{
    [Authorize]    
    public class MgmExamsController : Controller
    {

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExams BCExams { get; set; }

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExamQueries BCExamQueries { get; set; }

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCCustomers BCCustomers { get; set; }

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCCustomersQueries BCCustomersQueries { get; set; }

        // GET: /Administration/MgmExams/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Administration/MgmExams/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Administration/MgmExams/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Administration/MgmExams/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Administration/MgmExams/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Administration/MgmExams/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Administration/MgmExams/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }
        //
        // POST: /Administration/MgmExams/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [HttpGet]
        public ActionResult List()
        {            
            ViewData["Categories"] = this.BCExamQueries.SearchExamCategories(options: BusinessLogic.BusinessComponents.SearchExamCategoriesOptions.GetAll);
            return View();         
            
        }
        [HttpPost]
        public ActionResult List(Guid? CategoryId)
        {
            ActionResult result = null;
            ViewData["Categories"] = this.BCExamQueries.SearchExamCategories(options: BusinessLogic.BusinessComponents.SearchExamCategoriesOptions.GetAll);
            if (CategoryId == null)
            {
                var cat = ((IEnumerable<ExamCategory>)ViewData["Categories"]).FirstOrDefault();
                CategoryId = cat == null ? default(Guid?) : cat.Id;
            }

            var customer = this.BCCustomersQueries.FindCustomers(option: BusinessLogic.BusinessComponents.CustomerFindOptions.GetByUserName, UserName: this.User.Identity.Name).SingleOrDefault();
            var exams = this.BCExamQueries.SearchExams(options: BusinessLogic.BusinessComponents.ExamSearchOptions.GetAll);
            result = PartialView("ExamsGridView", exams.ToList());
            //if (!string.IsNullOrWhiteSpace(exams))
            //{
            //    var query = from c in XElement.Parse(exams).Descendants("Exam")
            //                select new CustomXmlExpandoObject()
            //                .BGuid("Id", c.Element("Id"))
            //                .BString("Title", c.Element("Title"))
            //                .BString("Summary", c.Element("Summary"));



            //    result = PartialView("ExamsGridView", query.ToList());
            //}
            //else
            //{
            //    result = PartialView("ExamsGridView", new List<CustomXmlExpandoObject>());
            //}
            return result;
        }
        [HttpGet]
        public ActionResult Detail(Guid? ExamId)
        {
            return View(new Exam()); 
        }

    
        protected override void OnException(ExceptionContext filterContext)
        {
            filterContext.ExceptionHandled = true;            
            bool rethrow = ExceptionPolicy.HandleException(filterContext.Exception, Infrastructure.Aspect.ExceptionHandling.ExceptionPolicyNames.WebControllerPolicy);
            if (rethrow)
            {
                throw filterContext.Exception;            
            }            
        }
    }
}
